<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <!--The viewport meta tag is used to improve the presentation and behavior of the samples 
    on iOS devices-->
  <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
  <title>Measure Polygon Area and Perimeter</title>
  
  <link rel="stylesheet" href="http://jsapi.thinkgis.cn/esri/css/esri.css">
  <style>
    html, body, #mapDiv {
      height: 100%;
      margin: 0;
      padding: 0;
      width: 100%;
    }
    #info {
      bottom: 20px;
      color: #444;
      height: auto;
      font-family: arial;
      left: 20px;
      margin: 5px;
      padding: 10px;
      position: absolute;
      text-align: left;
      width: 200px;
      z-index: 40;
    }
    .label {
      display: inline-block;
      width: 4em;
    }
  </style>

  <script src="http://jsapi.thinkgis.cn/init.js"></script>
  <script>
    require(["dojo/dom",
          "dojo/_base/lang",
          "dojo/json",
          "esri/config",
          "esri/map",
          "esri/graphic",
          "esri/geometry/Geometry",
          "esri/geometry/Extent",
          "esri/SpatialReference",
          "esri/tasks/GeometryService",
          "esri/tasks/AreasAndLengthsParameters",
          "esri/toolbars/draw",
          "esri/symbols/SimpleFillSymbol"],
    function(dom, lang, json, esriConfig, Map, Graphic, Geometry, Extent, SpatialReference, GeometryService, AreasAndLengthsParameters, Draw, SimpleFillSymbol){

      //identify proxy page to use if the toJson payload to the geometry service is greater than 2000 characters.
      //If this null or not available the project and lengths operation will not work.  Otherwise it will do a http post to the proxy.
      esriConfig.defaults.io.proxyUrl = "/proxy/";
      esriConfig.defaults.io.alwaysUseProxy = false;

      var map = new Map("mapDiv", {
        basemap: "topo",
        center: [-122.778, 45.483],
        zoom: 15
      });
      
      map.on("load", function() {
        var tb = new Draw(map);
        tb.on("draw-end", lang.hitch(map, getAreaAndLength));
        tb.activate(Draw.FREEHAND_POLYGON);
      });
      
      var geometryService = new GeometryService("http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/Geometry/GeometryServer");
      geometryService.on("areas-and-lengths-complete", outputAreaAndLength);

    function getAreaAndLength(evtObj) {
      var map = this,
          geometry = evtObj.geometry;
      map.graphics.clear();
      
      var graphic = map.graphics.add(new Graphic(geometry, new SimpleFillSymbol()));
      
      //setup the parameters for the areas and lengths operation
      var areasAndLengthParams = new AreasAndLengthsParameters();
      areasAndLengthParams.lengthUnit = GeometryService.UNIT_METER;
      areasAndLengthParams.areaUnit = GeometryService.UNIT_SQUARE_METERS;
      areasAndLengthParams.calculationType = "geodesic";
      geometryService.simplify([geometry], function(simplifiedGeometries) {
        areasAndLengthParams.polygons = simplifiedGeometries;
        geometryService.areasAndLengths(areasAndLengthParams);
      });
    }

    function outputAreaAndLength(evtObj) {
      var result = evtObj.result;
      //console.log(json.stringify(result));
      dom.byId("area").innerHTML = result.areas[0].toFixed(3) + " ㎡";
      dom.byId("length").innerHTML = result.lengths[0].toFixed(3) + " m";
    }
  });
  </script>

  </head>
  <body>
    <div id="mapDiv"></div>
    <div id="info" class="esriSimpleSlider">
      <span class="label">Area:</span> <span id="area"></span>
      <br>
      <span class="label">Length:</span> <span id="length"></span>
    </div>
  </body>
</html>